package itcast.bos.base.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import itcast.bos.base.service.IStandardService;
import itcast.bos.domain.base.Standard;

@Controller
@ParentPackage("json-default")
@Namespace("/")
@Scope(value = "prototype")
public class FindStandardByPage extends ActionSupport implements ModelDriven<Standard> {

	private int page;
	private int rows;

	@Autowired
	private IStandardService iss;

	// 保存
	@Action(value = "standardsave", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/standard.html"),
			@Result(name = "input", location = "/error.jsp"),
			})
	public String save() {
		System.out.println(123456789);
		System.out.println(standard);
		iss.save(standard);
		return "success";
	}

	// 分页查询
	@Action(value = "querybypage", results = { @Result(name = "success", type = "json") })
	public String querybypage() {
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<Standard> pageData = iss.findPageData(pageable);
		// 返回客户端需要的page 和 rows
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("page", pageData.getNumberOfElements());
		map.put("rows", pageData.getContent());
		// 返回json数据
		ActionContext.getContext().getValueStack().push(map);
		return "success";
	}

	// 查找Standard的方法
	@Action(value = "standard_findAll", results = { @Result(name = "success", type = "json") })
	public String findAll() {
		System.out.println(123465798);
		List<Standard> list=iss.findAll();
		ActionContext.getContext().getValueStack().push(list);
		return "success";
	}

	private Standard standard = new Standard();

	@Override
	public Standard getModel() {
		// TODO Auto-generated method stub
		return standard;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}
}
